package me.guyongquan.cal10npropwriter;

import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.EnumDeclaration;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclarationStatement;

public class EnumResourceVisitor extends ASTVisitor {
	private String baseName;
	private List<Location> locations = new LinkedList<Location>();
	private Map<String, String> charsets = Collections.emptyMap();
	public EnumResourceVisitor() {
	}
	
	@Override
	public boolean visit(EnumDeclaration node) {
		GetCharsetVisitor visitor = new GetCharsetVisitor();
		node.accept(visitor);
		charsets = visitor.charsets;
		return true;
	}
	
	@Override
	public boolean visit(org.eclipse.jdt.core.dom.SingleMemberAnnotation node) {
		String name = node.getTypeName().getFullyQualifiedName();
		if("ch.qos.cal10n.BaseName".equals(name) || "BaseName".equals(name)) {
			node.getValue().accept(new ASTVisitor() {
				public boolean visit(StringLiteral literal) {
					baseName = literal.getLiteralValue();
					return false;
				}
			});
		}
		return true;
	};
	
	@Override
	public boolean visit(org.eclipse.jdt.core.dom.LineComment node) {
		String content = source.substring(node.getStartPosition(), node.getStartPosition() + node.getLength());
		MessageElement message = new MessageElement();
		message.end = node.getStartPosition() + node.getLength();
		if(content.startsWith("//en:")) {
			message.locale = Locale.US;
			message.message = content.substring(5);
		} else if(content.startsWith("//cn:")) {
			message.locale = Locale.SIMPLIFIED_CHINESE;
			message.message = content.substring(5);
			locations.add(message);
		} else if(content.startsWith("//default:")) {
			message.locale = null;
			message.message = content.substring(10);
		} else {
			return true;
		}
		locations.add(message);
		return true;
	}
	
	@Override
	public boolean visit(CompilationUnit node) {
		List<Comment> commentList = (List<Comment>)node.getCommentList();
		for(Comment comment : commentList) {
			comment.accept(this);
		}
		return super.visit(node);
	}
	
	public String getBaseName() {
		return baseName;
	}
	
	public boolean write(File mainResourcesDir) {
		if(locations.isEmpty() || baseName == null) {
			return false;
		}
		Collections.sort(locations, new Comparator<Location>() {
			@Override
			public int compare(Location o1, Location o2) {
				return o2.end - o1.end;
			}
		});
		String key = null;
		PropertiesUpdater updater = new PropertiesUpdater(mainResourcesDir, baseName);
		for(Location location : locations) {
			if(location instanceof KeyElement) {
				key = ((KeyElement)location).name;
				updater.setCurrentKey(key);
				continue;
			}
			if(key == null) {
				continue;
			}
			MessageElement message = (MessageElement) location;
			updater.update(message.locale, message.message);
		}
		updater.save();
		return true;
	}
}
